/**
 * Copyright (c) 2013-Now http://jeesite.com All rights reserved.
 */
package com.jeesite.modules.base.web.customsdeclarationinfo;

import com.alibaba.fastjson.JSONValidator;
import com.jeesite.common.codec.EncodeUtils;
import com.jeesite.common.config.Global;
import com.jeesite.common.entity.Page;
import com.jeesite.common.web.BaseController;
import com.jeesite.modules.base.entity.customsdeclarationinfo.CustomsDeclarationInfo;
import com.jeesite.modules.base.entity.customsdeclarationinfo.CustomsDeclarationInfoDetail;
import com.jeesite.modules.base.service.customsdeclarationinfo.CustomsDeclarationInfoService;
import com.jeesite.modules.sys.entity.User;
import com.jeesite.modules.sys.service.UserService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.ModelAttribute;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.List;

/**
 * 报关资料Controller
 *
 * @author LV
 * @version 2021-03-02
 */
@Controller
@RequestMapping(value = "${adminPath}/base/customsdeclarationinfo/customsDeclarationInfo")
public class CustomsDeclarationInfoController extends BaseController {

    @Autowired
    private CustomsDeclarationInfoService customsDeclarationInfoService;
    @Autowired
    private UserService userService;

    /**
     * 获取数据
     */
    @ModelAttribute
    public CustomsDeclarationInfo get(String customsDeclarationInfoId, boolean isNewRecord) {
        return customsDeclarationInfoService.get(customsDeclarationInfoId, isNewRecord);
    }

    /**
     * 查询列表
     */
    @RequiresPermissions("base:customsdeclarationinfo:customsDeclarationInfo:view")
    @RequestMapping(value = {"list", ""})
    public String list(CustomsDeclarationInfo customsDeclarationInfo, Model model) {
        model.addAttribute("listUser", userService.findList(new User()));
        model.addAttribute("customsDeclarationInfo", customsDeclarationInfo);
        return "modules/base/customsdeclarationinfo/customsDeclarationInfoList";
    }

    /**
     * 查询列表数据
     */
    @RequiresPermissions("base:customsdeclarationinfo:customsDeclarationInfo:view")
    @RequestMapping(value = "listData")
    @ResponseBody
    public Page < CustomsDeclarationInfo > listData(CustomsDeclarationInfo customsDeclarationInfo, HttpServletRequest request, HttpServletResponse response) {
        customsDeclarationInfo.setPage(new Page <>(request, response));
        Page < CustomsDeclarationInfo > page = customsDeclarationInfoService.findPage(customsDeclarationInfo);
        return page;
    }

    /**
     * 查看编辑表单
     */
    @RequiresPermissions("base:customsdeclarationinfo:customsDeclarationInfo:view")
    @RequestMapping(value = "form")
    public String form(CustomsDeclarationInfo customsDeclarationInfo, Model model) {
        model.addAttribute("listUser", userService.findList(new User()));
        model.addAttribute("customsDeclarationInfo", customsDeclarationInfo);
        return "modules/base/customsdeclarationinfo/customsDeclarationInfoForm";
    }

    /**
     * 查看表单详情
     */
    @RequiresPermissions("base:customsdeclarationinfo:customsDeclarationInfo:view")
    @RequestMapping(value = "detail")
    public String detail(CustomsDeclarationInfo customsDeclarationInfo, Model model) {
        model.addAttribute("listUser", userService.findList(new User()));
        model.addAttribute("customsDeclarationInfo", customsDeclarationInfo);
        return "modules/base/customsdeclarationinfo/customsDeclarationInfoDetail";
    }

    /**
     * 保存数据
     */
    @RequiresPermissions("base:customsdeclarationinfo:customsDeclarationInfo:edit")
    @PostMapping(value = "save")
    @ResponseBody
    public String save(@Validated CustomsDeclarationInfo customsDeclarationInfo) {
        //资料列表不能为空
        List < CustomsDeclarationInfoDetail > list = customsDeclarationInfo.getCustomsDeclarationInfoDetailList();
        if (list == null || list.isEmpty()) {
            return renderResult(Global.FALSE, text("资料列表不能为空！"));
        }
        //同一版型、目的港不能有多个该档案记录
        CustomsDeclarationInfo declarationInfo = new CustomsDeclarationInfo();
        declarationInfo.setVersionType(customsDeclarationInfo.getVersionType());
        declarationInfo.setDestination(customsDeclarationInfo.getDestination());
        if (customsDeclarationInfo.getIsNewRecord()) {
            List < CustomsDeclarationInfo > listInfo = customsDeclarationInfoService.findList(declarationInfo);
            if (listInfo != null && !listInfo.isEmpty()) {
                return renderResult(Global.FALSE, text("同一版型、目的港不能有多个该档案记录！"));
            }
        } else {
            CustomsDeclarationInfo oldDeclarationInfo = customsDeclarationInfoService.getOneByID(customsDeclarationInfo.getCustomsDeclarationInfoId());
            if (!customsDeclarationInfo.getDestination().equals(oldDeclarationInfo.getDestination()) ||
                    !customsDeclarationInfo.getVersionType().equals(oldDeclarationInfo.getVersionType())) {
                List < CustomsDeclarationInfo > listInfo = customsDeclarationInfoService.findList(declarationInfo);
                if (listInfo != null && !listInfo.isEmpty()) {
                    return renderResult(Global.FALSE, text("同一版型、目的港不能有多个该档案记录！"));
                }
            }
        }
        customsDeclarationInfoService.save(customsDeclarationInfo);
        return renderResult(Global.TRUE, text("保存报关资料成功！"));
    }

    /**
     * 删除数据
     */
    @RequiresPermissions("base:customsdeclarationinfo:customsDeclarationInfo:delete")
    @RequestMapping(value = "delete")
    @ResponseBody
    public String delete(CustomsDeclarationInfo customsDeclarationInfo) {
        customsDeclarationInfoService.delete(customsDeclarationInfo);
        return renderResult(Global.TRUE, text("删除报关资料成功！"));
    }

    /**
     * 列表选择对话框
     */
    @RequiresPermissions("base:customsdeclarationinfo:customsDeclarationInfo:view")
    @RequestMapping(value = "customsDeclarationInfoSelect")
    public String empUserSelect(CustomsDeclarationInfo customsDeclarationInfo, String selectData, Model model) {
        String selectDataJson = EncodeUtils.decodeUrl(selectData);
        if (selectDataJson != null && JSONValidator.from(selectDataJson).validate()) {
            model.addAttribute("selectData", selectDataJson);
        }
        model.addAttribute("CustomsDeclarationInfo", customsDeclarationInfo);
        return "modules/base/customsdeclarationinfo/customsDeclarationInfoSelect";
    }

}